package org.example.test.service.impl;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.example.test.dal.entity.Product;
import org.example.test.dal.entity.User;
import org.example.test.dal.mapper.ProductMapper;
import org.example.test.dal.mapper.UserMapper;
import org.example.test.request.ProductRequest;
import org.example.test.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

@Component
public class ProductServiceImpl implements ProductService {
    private static final Logger LOGGER = LogManager.getLogger(ProductServiceImpl.class);

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public void addProduct(ProductRequest request) {

        User user = userMapper.selectByUserName(request.getUserId());

        Product product = new Product();
        product.setName(request.getName());
        product.setPrice(BigDecimal.valueOf(request.getPrice()));
        product.setStock(request.getStock());
        product.setDescription(request.getDescription());
        product.setCategoryId(request.getCategoryId());
        product.setUserId(user.getId());

        // 直接保存前端传过来的图片 URL
        if (request.getImage() != null && !request.getImage().isEmpty()) {
            product.setImagePath(request.getImage());
        }

        productMapper.insertSelective(product);
    }

    @Override
    public Product updateProduct(Long productId, ProductRequest productRequest) {
        Product product = productMapper.selectByPrimaryKey(productId);
        if (product == null) {
            throw new RuntimeException("Product not found");
        }

        product.setName(productRequest.getName());
        product.setPrice(BigDecimal.valueOf(productRequest.getPrice()));
        product.setStock(productRequest.getStock());
        product.setDescription(productRequest.getDescription());
        product.setCategoryId(productRequest.getCategoryId());

        // 直接更新图片 URL
        if (productRequest.getImage() != null && !productRequest.getImage().isEmpty()) {
            product.setImagePath(productRequest.getImage());
        }

        productMapper.updateByPrimaryKey(product);
        return product;
    }


    @Override
    public List<Product> searchProducts(String keyword) {
        try {
            // 直接从 product 表中查询，imageUrl 也会返回
            List<Product> products = productMapper.searchByKeyword(keyword);
            return products.isEmpty() ? Collections.emptyList() : products;
        } catch (Exception e) {
            LOGGER.error("searchProducts exception", e);
            throw new RuntimeException("Error searching products: " + e.getMessage());
        }
    }

    @Override
    public void deleteProduct(Long productId) {
        productMapper.deleteByPrimaryKey(productId);
    }

    @Override
    public List<Product> searchAll() {
        List<Product> products = productMapper.searchAll();
        return products;
    }

    @Override
    public Product searchByProductId(Long productId) {
        Product product = productMapper.selectByPrimaryKey(productId);
        return product;
    }

    @Override
    public List<Product> searchByCategory(Long categoryId) {
        List<Product> product = productMapper.searchByCategory(categoryId);
        return product;
    }

    @Override
    public List<Product> searchByUserId(Long userId) {
        User user = userMapper.selectByUserName(userId);
        List<Product> products = productMapper.searchByUserId(user.getId());
        return products;
    }
}
